package com.leminhtrinh.mycash.model;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper {

	// TRANSACTION TABLE
	public static final String TABLE_TRANSACTION = "transaction";
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_VALUE = "value";
	public static final String COLUMN_CATEGORY = "category_id";
	public static final String COLUMN_ACCOUNT = "acc_id";
	public static final String COLUMN_PARTNER = "partner";
	public static final String COLUMN_DESCRIPTION = "description";
	public static final String COLUMN_INOUT = "inout";
	public static final String COLUMN_YEAR = "year";
	public static final String COLUMN_MONTH = "month";
	public static final String COLUMN_DAY = "day";
	public static final String COLUMN_WEEKOFYEAR = "weekofyear";

	// ACCOUNT TABLE
	public static final String TABLE_ACCOUNT = "account";
	public static final String COLUMN_ACC_NAME = "name";
	public static final String COLUMN_ACC_BUDGET = "budget";

	// CATEGORY TABLE
	public static final String TABLE_CATEGORY = "category";
	public static final String COLUMN_CAT_NAME = "name";
	public static final String COLUMN_CAT_BUDGET = "budget";

	private static final String DATABASE_NAME = "cash.db";
	private static final int DATABASE_VERSION = 1;

	// Database creation sql statement
	private static final String DATABASE_CREATE_TRANSACTION = "create table "
			+ TABLE_TRANSACTION + "(" + COLUMN_ID
			+ " integer primary key autoincrement, " + COLUMN_VALUE
			+ " double not null, " + COLUMN_CATEGORY + " integer not null, "
			+ COLUMN_ACCOUNT + " integer not null, " + COLUMN_PARTNER
			+ " String, " + COLUMN_DESCRIPTION + " String, " + COLUMN_INOUT
			+ " interger not null, " + COLUMN_YEAR + " integer not null, "
			+ COLUMN_MONTH + " integer not null, " + COLUMN_DAY
			+ " integer not null, " + COLUMN_WEEKOFYEAR + " integer not null);";
	private static final String DATABASE_CREATE_ACCOUNT = "create table "
			+ TABLE_ACCOUNT + "(" + COLUMN_ID
			+ " integer primary key autoincrement, " + COLUMN_ACC_NAME
			+ " String not null, " + COLUMN_ACC_BUDGET + " double);";
	private static final String DATABASE_CREATE_CATEGORY = "create table "
			+ TABLE_CATEGORY + COLUMN_CAT_NAME + " String not null, "
			+ COLUMN_CAT_BUDGET + " double);";

	public MySQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(DATABASE_CREATE_TRANSACTION);
		database.execSQL(DATABASE_CREATE_ACCOUNT);
		database.execSQL(DATABASE_CREATE_CATEGORY);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(MySQLiteHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_TRANSACTION);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_ACCOUNT);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_CATEGORY);
		onCreate(db);
	}

}